package com.zl_huang.example.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zl_huang.example.dao.IExamTestTypeDaoHibernate;
import com.zl_huang.example.dao.IScoreDaoHibernate;
import com.zl_huang.example.dao.ISubjectDaoHibernate;
import com.zl_huang.example.dao.ITestDaoHibernate;
import com.zl_huang.example.model.ExamTestType;
import com.zl_huang.example.model.Score;
import com.zl_huang.example.model.Subject;
import com.zl_huang.example.model.Test;
import com.zl_huang.example.model.vo.ExamTestTypeVO;
import com.zl_huang.example.model.vo.TestVO;
import com.zl_huang.example.util.TestUtils;

@Service
public class TestServiceImpl implements ITestService {
	@Autowired
	private ITestDaoHibernate testDaoHibernate;
	@Autowired
	private ISubjectDaoHibernate subjectDaoHibernate;
	@Autowired
	private IExamTestTypeDaoHibernate  examTestTypeDaoHibernate;
	@Autowired
	private IScoreDaoHibernate  scoreDaoHibernate;
	

	@Override
	public Integer createTest(Test test) throws Exception {
		return testDaoHibernate.createTest(test);
	}

	@Override
	public Integer editTest(Test test) throws Exception {
		return testDaoHibernate.editTest(test);
	}

	@Override
	public Integer deleteTest(Integer id) throws Exception {
		return testDaoHibernate.deleteTest(id);
	}

	@Override
	public List<Test> findAllTest() throws Exception {
		return testDaoHibernate.findAllTest();
	}

	@Override
	public TestVO getTestVOById(Integer id) throws Exception {
		Test test = testDaoHibernate.getTestById(id);
		if(test != null){
			TestVO testVO = new TestVO();
			BeanUtils.copyProperties(test, testVO);
			Subject subject = subjectDaoHibernate.getSubjectById(test.getSub().getSub_id());
			if(subject != null){
				testVO.setSub_id(subject.getSub_id());
				testVO.setName(subject.getName());
			}
			testVO.setTestTypeId(test.getExamTestType().getId());
			testVO.setTestType(test.getExamTestType().getTestType());
			testVO.setTestTypeScore(test.getExamTestType().getTestTypeScore());
			return testVO;
			
		}
		return null;
	}

	@Override
	public Map<Integer, List<TestVO>> getTestListBySubject(Integer subId, String userId) throws Exception {
		//得到所有题型的类型列表
		List<ExamTestType> examTestTypeList = examTestTypeDaoHibernate.findAllExamTestTypet();
		//用于存储题型列表中的各个分值
		Map<Integer , Integer> examTestTypeScoreMap = new HashMap<Integer , Integer>();
		//用于存储各个题型类型的题目列表
		Map<Integer, List<TestVO>> testVOListMap = new HashMap<Integer, List<TestVO>>();
		
		if(!examTestTypeList.isEmpty()){
			//遍历题型列表，通过题型id和科目id来获取该类题型所有的题目
			for(ExamTestType examTestType : examTestTypeList){
				examTestTypeScoreMap.put(examTestType.getId(), examTestType.getTestTypeScore());
				//获取该科目所有这种题型的题目
				List<Test> testList = testDaoHibernate.getTestBySubIdAndExamTestTypeId(subId,examTestType.getId());
			    List<TestVO> testVOList  = new ArrayList<TestVO>();
			    for(Test test : testList){
			    	TestVO testVO = new TestVO();
			    	BeanUtils.copyProperties(test, testVO);
			    	ExamTestTypeVO examTestTypeVO = new ExamTestTypeVO();
			    	BeanUtils.copyProperties(test.getExamTestType(), examTestTypeVO);
			    	testVO.setExamTestTypeVO(examTestTypeVO);
			    	testVOList.add(testVO);
			    }
			    testVOListMap.put(examTestType.getId(), testVOList);
				
			}
		}else{
			return null;
		}
		Subject subject = subjectDaoHibernate.getSubjectById(subId);
		if(subject == null){
			return null;
		}
		Integer totalTestCount = subject.getTest_count();
		double totalScore = subject.getTotal_score();
		//校验科目总分和总题数是否匹配
		if(!TestUtils.checkSubjectMatchCountWithScore(totalTestCount, totalScore, examTestTypeScoreMap)){
			return null;
		}else{
		//获取各个题型的题目数量
			Map<Integer , Integer> TestCountByEachTestType = TestUtils.getTestCountByEachTestType(totalTestCount, examTestTypeScoreMap);
		//随机生成题目	
			Map<Integer , List<TestVO>> resultMap = new HashMap<Integer , List<TestVO>>();
			for(Integer key : TestCountByEachTestType.keySet()){
				//获取该题型的题目数量
				Integer count = TestCountByEachTestType.get(key);
				List<TestVO> testList = testVOListMap.get(key);
				//在testList里边随机取出count多道不重复的题目
				List<TestVO> resultTestList =  TestUtils.getRandomTestList(count , testList);				
				resultMap.put(key, resultTestList);
			}
			Score  getScore = scoreDaoHibernate.getScoreByUserIdAndSubId(userId, subId);
			if(getScore == null){
				Score score = new Score();
				score.setUser_id(userId);
				score.setSubject_id(subId);
				score.setExam_time(new Date());
				scoreDaoHibernate.save(score);
				scoreDaoHibernate.flush();
			}			
			return resultMap;
		}
		
		
	}

	@Override
	public Test getTestById(Integer id) throws Exception {		
		return testDaoHibernate.getTestById(id);
	}
	

}
